package com.da.javatest.niuke.baodian;

import java.util.Arrays;

/**
 * @author chenlida
 * @date 2020/10/13 11:11
 * https://www.nowcoder.com/practice/87d5a092a1d647479103e519a6c0a205?tpId=8&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C70FindPair {
    public int countPairs(int[] A, int n, int sum) {
        Arrays.sort(A);
        int count = 0;
        int mid = sum / 2;
        int length = A.length;
        for (int i = 0; i < length; i++) {
            if (A[i] > mid) {
                break;
            }
            for (int j = i + 1; j < length; j++) {
                if (A[j] > sum) {
                    break;
                }
                if (A[i] + A[j] == sum) {
                    count++;
                }
            }

        }
        return count;
    }
}
